<template>
  <div class="form-block">
    <el-form
      ref="form"
      :model="searchForm"
      :label-width="labelWidth"
      :inline="true"
      label-position="left"
    >
      <el-form-item
        v-for="(item, index) in searchFormTemp"
        :key="index"
        :label="item.lable"
      >
        <el-input
          v-if="item.type == 'input'"
          v-model="searchForm.param[item.name]"
          :placeholder="item.placeholder || '请输入'"
        ></el-input>
        <el-select
          v-if="item.type == 'select'"
          v-model="searchForm.param[item.name]"
          filterable
          :placeholder="item.placeholder || '请选择'"
        >
          <el-option
            v-for="(el, idx) in item.list"
            :key="idx"
            :label="el.dicItemName"
            :value="el.dicItemCode"
          ></el-option>
        </el-select>
        <el-date-picker
          v-if="item.type == 'datePicker'"
          v-model="searchForm.param[item.name]"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          clearable
          type="date"
          :placeholder="item.placeholder || '请选择'"
        ></el-date-picker>
        <doubleDate
          v-if="item.type === 'doubleDate'"
          :elType="item.elType"
          :options="item.options"
          :name="item.name ? item.name : ''"
          @editParams="getDateTime"
        ></doubleDate>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="normalSearch()">搜索</el-button>
        <el-button @click="normalResetQuery()" type="primary" plain
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import doubleDate from "@/components/doubleDate";
export default {
  name: "",
  props: {
    searchForm: {
      type: Object,
      default: function() {
        return {};
      }
    },
    searchFormTemp: {
      type: Array,
      default: function() {
        return [];
      }
    }
  },
  data() {
    return {};
  },
  components: {
    doubleDate
  },
  computed: {
    labelWidth() {
      return "auto";
    }
  },
  mounted() {
    this.$nextTick(() => {
      let el = document.querySelector(".el-form-item__label-wrap");
      if (el) {
        el.style.marginLeft = 0;
      }
    });
  },
  methods: {
    normalSearch() {
      this.handleSearchForm();
      this.$emit("normalSearch", this.searchForm);
    },
    normalResetQuery() {
      this.$refs.form.resetFields();
      this.$emit("normalResetQuery");
    },
    getDateTime(arr, name) {
      this.searchFormTemp.forEach(item => {
        if (item.type == "doubleDate") {
          if (item.name == name) {
            item.options[0].value = arr[0];
            item.options[1].value = arr[1];
          }
        }
      });
    },
    handleSearchForm() {
      let tempObj = {};
      for (let item of this.searchFormTemp) {
        if (item.type == "doubleDate") {
          for (let li of item.options) {
            tempObj[li.name] = li.value;
          }
        }
      }
      this.searchForm.param = Object.assign({}, this.searchForm.param, tempObj);
    }
  }
};
</script>

<style lang="less">
//固定输入框度
.form-block {
  background: #fff;
  padding-left: 40px;
  .el-input--medium {
    width: 215px !important;
  }
  .el-input__inner,
  .el-button--medium {
    border-radius: 6px;
  }
  .el-form-item__label {
    color: #333;
  }
}
</style>
